Imports BikiSoft.DataStream

Namespace Controls
    Public Class gaugeBar
        Implements DataStream.IStreamRecieve

        Private mStreamItemName As String = ""
        Private mInitialised As Boolean = False
        ' Private mColour As Color = Color.Blue

        Sub New(ByVal ItemName As String, ByVal Colour As Color)
            Me.InitializeComponent()

            ' Set the item name
            mStreamItemName = ItemName
            Me.Name = ItemName
            Me.uxBar.BarColour = Colour

            ' Initialise the control
            InitialiseControl()
        End Sub

        Public Property StreamItemName() As String
            Get
                Return mStreamItemName
            End Get
            Set(ByVal value As String)
                mStreamItemName = value
                mInitialised = False
                ' Initialise the control
                InitialiseControl()
                ' Request a refresh if one can be done
                RaiseEvent RequestRefresh(Me, New System.EventArgs)
            End Set
        End Property

        Public Property Colour() As Color
            Get
                Return Me.uxBar.BarColour
            End Get
            Set(ByVal value As Color)
                Me.uxBar.BarColour = value
            End Set
        End Property

        ''' <summary>
        ''' Initialises the control to default values
        ''' </summary>
        ''' <remarks></remarks>
        Private Sub InitialiseControl()
            uxLabel.Text = mStreamItemName
            uxBar.Minimum = 0
            uxBar.Maximum = 100
            uxBar.Value = 0
            uxBar.Caption = "n/a"
        End Sub
        ''' <summary>
        ''' Initialises the control bar from the first instance passed to the control
        ''' </summary>
        ''' <param name="Item"></param>
        ''' <remarks></remarks>
        Private Sub InitialiseControl(ByVal Item As DataStream.StreamItem)
            uxBar.Maximum = Item.ItemMax
            uxBar.Minimum = Item.ItemMin
        End Sub

        ''' <summary>
        ''' Handles stream data arriving
        ''' </summary>
        ''' <param name="Stream"></param>
        ''' <remarks></remarks>
        Public Sub RecieveStream(ByVal Stream As DataStream.Stream) Implements DataStream.IStreamRecieve.RecieveStream
            If Stream.contains(mStreamItemName) Then
                Dim item As DataStream.StreamItem = Stream(mStreamItemName)
                Dim value As Single
                Dim caption As String = ""

                ' Initialise if required
                If Not mInitialised Then
                    InitialiseControl(item)
                End If

                ' Set the values
                '  If item.Value >= uxBar.Minimum And item.Value <= uxBar.Maximum And item.Value <> uxBar.Value Then
                value = item.Value
                'End If

                caption = item.FormatedValue

                ' Set the bar
                uxBar.SetBar(value, caption)

            Else
                If uxBar.Value <> uxBar.Minimum Then
                    uxBar.SetBar(uxBar.Minimum, "n/a")
                End If
            End If
        End Sub

        Public Event RequestRefresh(ByVal sender As Object, ByVal e As System.EventArgs) Implements DataStream.IStreamRecieve.RequestRefresh

        Public Event Restrict(ByVal sender As Object, ByVal e As DataStream.StreamRestrictEventArgs) Implements DataStream.IStreamRecieve.Restrict

        Public Event Seek(ByVal sender As Object, ByVal e As DataStream.StreamSeekEventArgs) Implements DataStream.IStreamRecieve.Seek


        Public Event Close(ByVal sender As Object, ByVal e As System.EventArgs) Implements DataStream.IStreamRecieve.Close

        Public Sub RecieveSeek(ByVal Stream As DataStream.Stream) Implements DataStream.IStreamRecieve.RecieveSeek
            Me.RecieveStream(Stream)
        End Sub

        Public ReadOnly Property Name1() As String Implements INamed.Name
            Get
                Return Me.Name
            End Get
        End Property

        Public ReadOnly Property RecieverType() As DataStream.StreamRecieveType Implements DataStream.IStreamRecieve.RecieverType
            Get
                Return DataStream.StreamRecieveType.eStreamConsumeOnly
            End Get
        End Property
    End Class
End Namespace